package com.ceba.test.modules.regist.service;

import java.util.Date;
import java.util.Map;

import com.ceba.base.helper.DBJSONHelper;
import com.ceba.base.web.service.IBaseService;
import com.ceba.test.modules.regist.dao.EnterpriseRegistDao;
import com.ceba.test.modules.regist.entity.EnterpriseRegist;

import abc.data.DataTable;

public class EnterpriseRegistService extends IBaseService {
	
	
	private EnterpriseRegistDao enterpriseRegistDao;
	
	/**
	 * 添加一条记录，默认试用日期从激活当日起3个月,签名份数3000
	 * @return
	 */
	public Boolean add(EnterpriseRegist enterprise) throws Exception{
		return enterpriseRegistDao.add(enterprise);
	}
	
	
	/**
	 * 删除一条数据
	 * @param id 数据id
	 * @return
	 */
	public Boolean delete(String id) throws Exception{
		return enterpriseRegistDao.delete(id);
	}
	
	public Boolean deleteByName(String enterpriseName) throws Exception{
		
		return enterpriseRegistDao.deleteByName(enterpriseName);
	}
	
	public Boolean updateByName(String enterpriseName,String version,String signedCount, String alreadyActiveTimes) throws Exception{
	return enterpriseRegistDao.updateByName(enterpriseName, version, signedCount, alreadyActiveTimes);
	}
	
	public Boolean updateServer(EnterpriseRegist enterpriseRegist) 
					throws Exception{
		
		return enterpriseRegistDao.updateServer(enterpriseRegist);
	}
	
	
	public EnterpriseRegist getEnterpriseRegistByName(String enterpriseName)throws Exception{
		DataTable dt = enterpriseRegistDao.getEnterpriseRegistByName(enterpriseName);
		if(dt !=null && dt.getRowCount() >0){
			EnterpriseRegist er = new EnterpriseRegist();
			er.setEnterpriseName(dt.getRow(0).getString("ENTERPRISE_NAME"));
			er.setAvailableDate(dt.getRow(0).getString("AVALIABLE_DATE"));
			er.setSignCount(dt.getRow(0).getString("SIGN_COUNT"));
			er.setNewAvailableDate(dt.getRow(0).getString("NEW_AVALIABLE_DATE"));
			er.setNewSignCount(dt.getRow(0).getString("NEW_SIGN_COUNT"));
			er.setSignedCount(dt.getRow(0).getString("SIGNED_COUNT"));
			er.setUpdateFlag(dt.getRow(0).getString("UPDATE_FLAG"));
			er.setVersion(dt.getRow(0).getString("VERSION"));
			Date createTime = dt.getRow(0).getDate("CREATE_TIME");
			if( createTime !=null){
				er.setCreateTime(dt.getRow(0).getDate("CREATE_TIME").toString());
			}
			er.setSystemType(dt.getRow(0).getString("SYSTEM_TYPE").toString());
			Date formalUseTime = dt.getRow(0).getDate("FORMAL_USE_TIME");
			if( formalUseTime !=null){
				er.setFormalUseTime(dt.getRow(0).getDate("FORMAL_USE_TIME").toString());
			}
			er.setCollectionSqlFlag(dt.getRow(0).getString("COLLECT_SQL_FLAG"));
			er.setAlreadyActiveTimes(dt.getRow(0).getString("ALREADY_ACTIVE_TIMES"));
			er.setActiveTimes(dt.getRow(0).getString("ACTIVE_TIMES"));
			er.setFeeType(dt.getRow(0).getString("FEE_TYPE"));
			return er;
		}else{
			return null;
		}
		
	}


	public boolean updateFormalUseTimeByName(String enterpriseName,String time) {
		
		return enterpriseRegistDao.updateFormalUseTimeByName(enterpriseName, time);
	}


	public String queryEnterpriseRegist(String key, Map<String, Object> dic) {
		DataTable dt  = enterpriseRegistDao.queryEnterpriseRegist(key,dic);
		if(dt !=null && dt.getRowCount()>0){
			return dt.toJSON();
		}else{
			return DBJSONHelper.getEmptyDBJSONString();
		}
	}


	public String queryEnterpriseRegistLogList(String key, Map<String, Object> dic) {
		DataTable dt  = enterpriseRegistDao.queryEnterpriseRegistLogList(key,dic);
		if(dt !=null && dt.getRowCount()>0){
			return dt.toJSON();
		}else{
			return DBJSONHelper.getEmptyDBJSONString();
		}
	}
	
	

	public EnterpriseRegistDao getEnterpriseRegistDao() {
		return enterpriseRegistDao;
	}


	public void setEnterpriseRegistDao(EnterpriseRegistDao enterpriseRegistDao) {
		this.enterpriseRegistDao = enterpriseRegistDao;
	}
	
}
